package com.shfl.Controller;

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;

import com.shfl.Service.UserInfoService;
import com.shfl.app.One;
import com.shfl.app.Second;
import com.shfl.model.ResultType;
import com.shfl.model.WokingTime;
import com.shfl.stock.Stock;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Window;

public class MainController implements Initializable {

	@FXML
	private Button selectFile;

	@FXML
	private TextField showFileName;

	@FXML
	private TextField defaultStartTime;// 默认开始时间

	@FXML
	private TextField defaultEndTime;// 默认结束时间

	@FXML
	private TextField AearlyStratTime;// A组早班开始时间

	@FXML
	private TextField AearlyEndTime;// A组早班结束时间

	@FXML
	private TextField AnightStratime;// A组晚班开始时间

	@FXML
	private TextField AnightEndTime;// A组晚班结束时间

	@FXML
	private TextField BearlyStratTime;// B组早班开始时间

	@FXML
	private TextField BearlyEndTime;// B组早班结束时间

	@FXML
	private TextField BnightStartTime;// B组晚班开始时间

	@FXML
	private TextField BnightEndTime;// B组晚班结束时间

	@FXML
	private TextArea AmorningShift;// A早晚班职位

	@FXML
	private TextArea BmorningShift;// B早晚班职位

	@FXML
	private Button firstStep;// 第一步

	@FXML
	private TextArea userLog;// 人员导入

	@FXML
	private ChoiceBox AmorningShiftBox;// 下拉选择

	@FXML
	private ChoiceBox BmorningShiftBox;// 下拉选择

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void initialize(URL location, ResourceBundle resources) {
		

		
		
		
		
		
		// 选择文件
		this.selectFile.setOnAction(event -> {
			FileChooser fileChooser = new FileChooser();
			// 只能够获取的文件
			fileChooser.getExtensionFilters().addAll(
					new FileChooser.ExtensionFilter("XLSX", "*.xlsx"),
					new FileChooser.ExtensionFilter("XLS", "*.xls"));

			Window window = ((Node) event.getSource()).getScene().getWindow();
			File file = fileChooser.showOpenDialog(window);
			if (file != null) {
				this.showFileName.setText(file.getAbsolutePath());
				UserInfoService userInfoService = new UserInfoService();
				ResultType resultType = userInfoService.saveUserInfo(file).getDate();
				this.userLog.setText(resultType.getMsg());
				if (resultType.getCode().equals("0000")) {
					this.AmorningShiftBox.setItems(FXCollections.observableArrayList(Stock.JOB_SET));
					this.BmorningShiftBox.setItems(FXCollections.observableArrayList(Stock.JOB_SET));
				}

			}
		});

		// 第一步
		this.firstStep.setOnAction(event -> {
			if(!Stock.FALG){
				this.userLog.setText("缺少必要员工！！！！！！！！！！！！！！！！！！！");
				return ;
			}
			Stock.DEFAULT_WOKING_TIME.setStartTime(this.defaultStartTime.getText());
			Stock.DEFAULT_WOKING_TIME.setEndTime(this.defaultEndTime.getText());

			WokingTime AwokingTime = new WokingTime();

			AwokingTime.setEarlyStartTime(this.AearlyStratTime.getText());
			AwokingTime.setEarlyEndTime(this.AearlyEndTime.getText());
			AwokingTime.setNightStartTime(this.AnightStratime.getText());
			AwokingTime.setNightEndTime(this.AnightEndTime.getText());

			WokingTime BwokingTime = new WokingTime();
			BwokingTime.setEarlyStartTime(this.BearlyStratTime.getText());
			BwokingTime.setEarlyEndTime(this.BearlyEndTime.getText());
			BwokingTime.setNightStartTime(this.BnightStartTime.getText());
			BwokingTime.setNightEndTime(this.BnightEndTime.getText());
			Stock.JOB_TIME_LIST.add(AwokingTime);
			Stock.JOB_TIME_LIST.add(BwokingTime);

			String[] AmorningShiftA = this.AmorningShift.getText().split(",");

			for (String str : AmorningShiftA) {
				if (null != str && !str.isEmpty()) {
					Stock.JOB_TIME_MAP.put(str, AwokingTime);
				}
			}
			String[] BmorningShiftA = this.BmorningShift.getText().split(",");
			for (String str : BmorningShiftA) {
				if (null != str && !str.isEmpty()) {
					Stock.JOB_TIME_MAP.put(str, BwokingTime);
				}
			}
			try {
				changeWindow();
			} catch (Exception e) {
				e.printStackTrace();
			}

		});

		this.AmorningShiftBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue, Object newValue) {
				Set<String> setA = new HashSet<String>(Arrays.asList(AmorningShift.getText().split(",")));
				Set<String> setB = new HashSet<String>(Arrays.asList(BmorningShift.getText().split(",")));

				if (setA.add(newValue.toString()) && setB.add(newValue.toString())) {
					AmorningShift.setText(AmorningShift.getText() + newValue.toString() + ",");
				}
			}
		});
		this.BmorningShiftBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue, Object newValue) {
				Set<String> setA = new HashSet<String>(Arrays.asList(AmorningShift.getText().split(",")));
				Set<String> setB = new HashSet<String>(Arrays.asList(BmorningShift.getText().split(",")));

				if (setA.add(newValue.toString()) && setB.add(newValue.toString())) {
					BmorningShift.setText(BmorningShift.getText() + newValue.toString() + ",");
				}
			}
		});
	}

	public void changeWindow() throws Exception {
		Stock.SECOND.showWindow();
		Stock.ONE.stop();
	}

}
